﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.Provider;
using CE.Global;

public partial class Admin_provider_AddNewProviderWork : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCIWRKSHP";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!Page.IsPostBack)
        {
            //DataTable _dtCompany = General.GetComboItem("ProviderCompany", false);
            //ddlProviderCompanyName.DataSource = _dtCompany;
            //ddlProviderCompanyName.DataTextField = _dtCompany.Columns[0].ColumnName.ToString();
            //ddlProviderCompanyName.DataValueField = _dtCompany.Columns[1].ColumnName.ToString();
            //ddlProviderCompanyName.DataBind();

            //ddlProviderCompanyName.Items.Insert(0, new ListItem("Select", "-1"));
            //ddlProviderCompanyName.Text = Session[General.ProviderCompanyId].ToString();
            //ddlProviderCompanyName.Enabled = false;
            if (Page.Request.Params["WSId"] != null)
            {
                Int64 _workshopId = 0;
                if (Int64.TryParse(Page.Request.Params["WSId"].ToString(),out _workshopId))
                {
                    ShowWorkShopDetail(_workshopId);
                }
            }
        }
    }

    private void ShowWorkShopDetail(Int64 WorkShopId)
    {
        ClearForm();
        ProviderWorkShop _workshop = new ProviderWorkShop();
        ProviderWorkShopDB _workshopdb = new ProviderWorkShopDB();
        _workshop = _workshopdb.GetProviderWorkShop(WorkShopId);
        if (_workshop != null)
        {
            lblProviderWorkshopId.Text = WorkShopId.ToString();
            //ddlProviderCompanyName.Text = _workshop.ProviderCompanyId.ToString();
            txtWorkShopName.Text = _workshop.WSName;
            txtBlockNo.Text = _workshop.BlockNo;
            txtAreaName.Text = _workshop.AreaName;
            txtBuildingName.Text = _workshop.BuildingName;
            txtLevelNo.Text = _workshop.LevelNo;
            txtRoomNo.Text = _workshop.RoomNo;
            txtStreetName.Text = _workshop.StreetName;
            txtPostCode.Text = _workshop.PostCode;
            ddlZone.Text = _workshop.ZoneName;
            txtCountry.Text = _workshop.Country;
            chkWorkShopStatus.Checked = _workshop.Status;
            txtPhoneNum.Text = _workshop.Info1;
            //btnSubmit.Text = "Update";
        }
    }

    protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null)
        {
            if (Page.IsValid)
            {
                string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
                if (CheckAuthCode(_authcode, 100))
                {
                    ProviderWorkShop _workshop = new ProviderWorkShop();
                    ProviderWorkShopDB _workshopdb = new ProviderWorkShopDB();
                    if (lblProviderWorkshopId.Text.Length > 0)
                    {
                        Int64 _workshopId = 0;
                        if (Int64.TryParse(lblProviderWorkshopId.Text, out _workshopId))
                        {
                            _workshop.WorkShopId = _workshopId;
                        }
                    }

                    int _trantype = 1;

                    if (_workshop.WorkShopId > 0)
                    {
                        _trantype = 2;
                    }

                    _workshop.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
                    _workshop.WSName = txtWorkShopName.Text;
                    _workshop.BlockNo = txtBlockNo.Text;
                    _workshop.AreaName = txtAreaName.Text;
                    _workshop.BuildingName = txtBuildingName.Text;
                    _workshop.LevelNo = txtLevelNo.Text;
                    _workshop.RoomNo = txtRoomNo.Text;
                    _workshop.StreetName = txtStreetName.Text;
                    _workshop.PostCode = txtPostCode.Text;
                    _workshop.ZoneName = ddlZone.Text;
                    _workshop.Country = txtCountry.Text;
                    _workshop.Status = chkWorkShopStatus.Checked;
                    _workshop.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());

                    _workshop.Info1 = txtPhoneNum.Text;

                    _workshop.WorkShopId = _workshopdb.Insert(_workshop);

                    if (_workshop.WorkShopId > 0)
                    {
                        if (lblProviderWorkshopId.Text.Length > 0)
                        {
                            InsertProviderAuthTransaction(_workshop.WorkShopId, "CICarWorkshop", _trantype, 100, _authcode, _workshop.ProviderCompanyId, _workshop.RefCEUserId);
                            lblMessage.Text = "Update successfully!";
                        }
                        else
                        {
                            lblMessage.Text = "Insert successfully!";
                        }
                        Response.Redirect("ViewProviderWorkshop.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Inserting failed!";
                    }
                }
                else
                {
                    lblMessage.Text = "Wrong auth code!";
                }
            }
        }
        else
        {
            Response.Redirect("AdminSecureLogin.aspx");
        }
    }

    private void ClearForm()
    {
        lblProviderWorkshopId.Text = "0";
        //ddlProviderCompanyName.Text = "-1";
        txtWorkShopName.Text = "";
        txtBlockNo.Text = "";
        txtAreaName.Text = "";
        txtBuildingName.Text = "";
        txtLevelNo.Text = "";
        txtRoomNo.Text = "";
        txtStreetName.Text = "";
        txtPostCode.Text = "";
        ddlZone.Text = "-1";
        txtCountry.Text = "Singapore";
        lblMessage.Text = "";
        //btnSubmit.Text = "Submit";
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderWorkshop.aspx");
    }

    protected void btnDelete_Click(object sender, ImageClickEventArgs e)
    {
        rfvAuthCode.Validate();
        if (lblProviderWorkshopId.Text != "0" && txtAuthCode.Text.Trim().Length > 0)
        {
            //Int64 _caryearid = CheckCarYear(Convert.ToInt32(ddlFromYear.Text), Convert.ToInt32(ddlToYear.Text), Convert.ToInt32(ddlOccupation.Text));
            Int64 _workshopid = Convert.ToInt64(lblProviderWorkshopId.Text);
            Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());
            if (CheckAuthCode(_authcode, 100))
            {
                if (_workshopid != 0)
                {
                    ProviderWorkShopDB _workshopdb = new ProviderWorkShopDB();
                    if (_workshopdb.Delete(_workshopid, _providercompanyid))
                    {
                        InsertProviderAuthTransaction(_workshopid, "CICarWorkshop", 3, 100, _authcode, _providercompanyid, _refceuserid);
                        Response.Redirect("ViewProviderWorkshop.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Deleting failed.";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    protected void btnClearForm_Click(object sender, ImageClickEventArgs e)
    {
        ClearForm();
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            btnSubmit.Enabled = false;
            btnClearForm.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            btnSubmit.Enabled = true;
            btnClearForm.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnSubmit.Enabled = true;
            btnClearForm.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
